package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.bean.main.DetailByCarBrandAndQuestionBean;
import com.das.mechanic_base.app.X3RouterCenter;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3Utils;

import java.util.List;

public class X3DetectionAloneItemAdapter extends RecyclerView.Adapter<X3DetectionAloneItemAdapter.DetectionAloneItemHolder> {

    private List<DetailByCarBrandAndQuestionBean.BrandAmountVoBean> mList;

    private Context mContext;

    /**
     * 0 车辆品牌
     * 1 检测项/检测点
     */
    private int type;

    /**
     * type == 0  showType 0 百分比  showType 1 数值
     * type == 1  showType 0 检测项  showType 1 检测点
     */
    /**
     * type == 0 车型分布
     * type == 1 检测项
     * type == 2 检测点
     * showType == 0 百分比
     * showType == 1 数值
     */
    private int showType;

    public X3DetectionAloneItemAdapter(List<DetailByCarBrandAndQuestionBean.BrandAmountVoBean> mList, Context mContext, int type, int showType) {
        this.mList = mList;
        this.mContext = mContext;
        this.type = type;
        this.showType = showType;
    }

    @NonNull
    @Override
    public DetectionAloneItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new DetectionAloneItemHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_detection_alone_item_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull DetectionAloneItemHolder holder, int position) {

        holder.tv_index.setText((position + 1) + "");

        holder.v_line.setVisibility((position == mList.size() - 1) ? View.GONE : View.VISIBLE);

        switch (type) {
            case 0:
                String carBrandName = X3StringUtils.isEmpty(mList.get(position).carBrandName) ? " -" : mList.get(position).carBrandName;
                holder.tv_content.setText(carBrandName);
                if (showType == 0) {
                    int percentValue = (int) (mList.get(position).percentValue * 100);
                    holder.tv_num.setText(percentValue + "%");

                }else {
                    holder.tv_num.setText(mList.get(position).carBrandAmount + "");

                }

                break;
            case 1:
                if (showType == 0) {
                    String questionToItemName = X3StringUtils.isEmpty(mList.get(position).questionToItemName) ? " -" : mList.get(position).questionToItemName;
                    holder.tv_content.setText(questionToItemName);
                    if (showType == 0) {
                        int percentValue = (int) (mList.get(position).percentValue * 100);
                        holder.tv_num.setText(percentValue + "%");

                    }else {
                        holder.tv_num.setText(mList.get(position).questionToItemAmount + "");

                    }
                }else {
                    String questionBaseName = X3StringUtils.isEmpty(mList.get(position).questionBaseName) ? " -" : mList.get(position).questionBaseName;
                    holder.tv_content.setText(questionBaseName);

                    if (showType == 0) {
                        int percentValue = (int) (mList.get(position).percentValue * 100);
                        holder.tv_num.setText(percentValue + "%");

                    }else {
                        holder.tv_num.setText(mList.get(position).questionBaseAmount + "");

                    }
                }
                break;
        }

        GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(X3StringUtils.getDetectionColor(position));
        drawable.setShape(GradientDrawable.OVAL);
        holder.v_shape.setBackground(drawable);

        holder.itemView.setOnClickListener(v -> {
            if (!X3Utils.isFastClick()) {
                return;
            }
            if (type == 1 && ((showType == 1 && mList.get(position).questionBaseId == 0) || (showType == 0 && mList.get(position).questionToItemId == 0))) {
                return;
            }
            if (type == 0 && mList.get(position).carBrandId == 0) {
                return;
            }
//            Intent intent = new Intent(mContext, GroundPushActivity.class);
//            intent.putExtra("isDark",true);
//            if (type == 0) {
//                intent.putExtra("path", Constent.MINE_SHOW_DETAIL + "?name=" + mList.get(position).carBrandName.replace("%","%25") + "&type=" + 1 + "&amount=" + mList.get(position).carBrandAmount);
//
//            }else if (type == 1){
//                if (showType == 0) {
//                    intent.putExtra("path", Constent.MINE_SHOW_DETAIL + "?name=" + mList.get(position).questionToItemName.replace("%","%25") + "&type=" + 2);
//
//                }else {
//                    intent.putExtra("path", Constent.MINE_SHOW_DETAIL + "?name=" + mList.get(position).questionBaseName.replace("%","%25") + "&type=" + 3);
//
//                }
//            }
//            intent.putExtra("title","查看有效检测和常见异常页");
//            mContext.startActivity(intent);

            Bundle bundle = new Bundle();
            bundle.putBoolean("isDark",true);
            bundle.putString("title","查看有效检测和常见异常页");
            if (type == 0) {
                bundle.putString("path", X3Constent.MINE_SHOW_DETAIL + "?name=" + mList.get(position).carBrandName.replace("%","%25") + "&type=" + 1 + "&amount=" + mList.get(position).carBrandAmount);

            }else if (type == 1){
                if (showType == 0) {
                    bundle.putString("path", X3Constent.MINE_SHOW_DETAIL + "?name=" + mList.get(position).questionToItemName.replace("%","%25") + "&type=" + 2);

                }else {
                    bundle.putString("path", X3Constent.MINE_SHOW_DETAIL + "?name=" + mList.get(position).questionBaseName.replace("%","%25") + "&type=" + 3);

                }
            }
            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH,bundle);
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class DetectionAloneItemHolder extends RecyclerView.ViewHolder{
        TextView tv_index;
        TextView tv_content;
        View v_shape;
        TextView tv_num;
        View v_line;
        public DetectionAloneItemHolder(@NonNull View itemView) {
            super(itemView);
            tv_index = itemView.findViewById(R.id.tv_index);
            tv_content = itemView.findViewById(R.id.tv_content);
            v_shape = itemView.findViewById(R.id.v_shape);
            tv_num = itemView.findViewById(R.id.tv_num);
            v_line = itemView.findViewById(R.id.v_line);
        }
    }
}
